//
//  HomeController.m
//  OCDemo
//
//  Created by zhumuzhi on 2021/3/9.
//

#import "HomeController.h"

// UI
#import "EnterButton.h"
#import "BYHomeBannerView.h"
#import "HomeCell.h"
// Data
#import "DBProduct.h"
#import "DBProductDetail.h"
#import "DBUser.h"
// Model
#import "ProductModel.h"
#import "BYProductDetailsModel.h"

#import "BYProductDetailsController.h"
#import "BYSearchController.h"

@interface HomeController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray    *dataList;
@property (nonatomic, strong) BYHomeBannerView  *bannerView;
@property (nonatomic, strong) BYTableView       *tableView;

@end

@implementation HomeController

static NSString *const cellIdentifierID = @"HomeCellID";

#pragma mark - Event
- (void)searchEvent {
    NSLog(@"搜索事件");
    
    BYSearchController *searchController = [[BYSearchController alloc] init];
    searchController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:searchController animated:YES];
}

#pragma mark - RequestData

- (void)getProductData{
    [self dbDataOperation];
    self.dataList = [ProductModel mj_objectArrayWithKeyValuesArray:[kUserDefaultsSington valueForKey:kProductKey]];
    [self.tableView reloadData];
    [self.tableView.mj_header endRefreshing];
}

#pragma mark - lifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(searchEvent)];
        self.navigationItem.rightBarButtonItem = rightItem;

    
    [self.view addSubview:self.tableView];
    
    [self getProductData];
    [self.tableView.mj_header beginRefreshing];
    
    [kNotificationCenterSington addObserver:self selector:@selector(getProductData) name:kFSNotificationNameLoginSuccess object:nil];
    
    NSLog(@"Path：%@", [NSString  getDocumentPath]);   
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1 ;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierID];
    ProductModel *model = self.dataList[indexPath.row];
    cell.productModel = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [HomeCell cellHeight];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ProductModel *model = self.dataList[indexPath.row];
    BYProductDetailsController *controlelr = [[BYProductDetailsController alloc] init];
    controlelr.detailsProductId = model.product_id;
    controlelr.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:controlelr animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark - LazyGet

- (NSMutableArray *)dataList {
    if (!_dataList) {
        _dataList = [NSMutableArray array];
    }
    return _dataList;
}

- (BYTableView *)tableView {
    if (!_tableView) {
        _tableView = [[BYTableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-kSafeAreaTopHeight-kTabBarHeight-kSafeAreaBottomHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView registerClass:[HomeCell class] forCellReuseIdentifier:cellIdentifierID];
//        WS(weakSelf)
//        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//            [weakSelf getProductData];
//        }];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableHeaderView = self.bannerView;
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0.0f;
        }
    }
    return _tableView;
}

- (BYHomeBannerView *)bannerView {
    if (!_bannerView) {
        _bannerView = [[BYHomeBannerView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, [BYHomeBannerView viewHeight])];
        NSArray *modelArray = [BYProductDetailsModel mj_objectArrayWithKeyValuesArray:[kUserDefaultsSington valueForKey:kProductDetail]];
        NSMutableArray *tempImages = [NSMutableArray arrayWithCapacity:0];
        for (NSInteger i = 0; i<modelArray.count; i++) {
            BYProductDetailsModel *model = modelArray[i];
            [tempImages addObject:model.topImage1];
        }
        _bannerView.images = tempImages;
    }
    return _bannerView;
}

- (void)dbDataOperation {
    // 在数据库中查询所有商品的数据，并保缓存到plist文件之中
    [[DBProduct sharedInstance] LinkDatabaseAndAddToQueue];
    [[DBProduct sharedInstance] insetProduct_Id:@"101" WithProduct_Name:@"保时捷911GT3" WithProduct_IconImage:@"p01" WithProduct_CategoryID:@"成人" WithProduct_Price:@"3499" WithProduct_MarketPrice:@"2899" WithProduct_Description:@"此 产品适合16岁以上的人群，带有换挡拨片活动方向盘，可调式后扰流板，可打开的引擎盖、车门，带RS徽章的原创设计轮毂"];
    [[DBProduct sharedInstance] insetProduct_Id:@"102" WithProduct_Name:@"豪华千年隼" WithProduct_IconImage:@"p02" WithProduct_CategoryID:@"儿童" WithProduct_Price:@"1088" WithProduct_MarketPrice:@"988" WithProduct_Description:@"可以动的舰体板，可发射激光炮的舰体，可发射激光炮的中心顶盖"];
    [[DBProduct sharedInstance] insetProduct_Id:@"103" WithProduct_Name:@"机场救援车" WithProduct_IconImage:@"p03" WithProduct_CategoryID:@"儿童" WithProduct_Price:@"759" WithProduct_MarketPrice:@"679" WithProduct_Description:@"待精致仪表盘的驾驶舱，带水炮的多功能伸缩臂，带移动活塞的四缸引擎"];
    [[DBProduct sharedInstance] insetProduct_Id:@"104" WithProduct_Name:@"马克卡车MSCK An Them" WithProduct_IconImage:@"p04" WithProduct_CategoryID:@"学龄前" WithProduct_Price:@"1699" WithProduct_MarketPrice:@"1499" WithProduct_Description:@"配有可伸缩的斜撑、支架撑和同步伸缩式起重臂的拖车"];
    [[DBProduct sharedInstance] insetProduct_Id:@"105" WithProduct_Name:@"城市警察总局 拼接积木" WithProduct_IconImage:@"p05" WithProduct_CategoryID:@"城市" WithProduct_Price:@"1099" WithProduct_MarketPrice:@"899" WithProduct_Description:@"追踪重型拖车、酷炫警车以及警察局"];
    [[DBProduct sharedInstance] insetProduct_Id:@"106" WithProduct_Name:@"芝加哥 成人粉丝拼装建筑积木" WithProduct_IconImage:@"p06" WithProduct_CategoryID:@"建筑" WithProduct_Price:@"599" WithProduct_MarketPrice:@"389" WithProduct_Description:@"建筑系列 芝加哥 拼装建筑积木玩具收藏"];
    [[DBProduct sharedInstance] insetProduct_Id:@"107" WithProduct_Name:@"冬季村庄车站 创意百变" WithProduct_IconImage:@"p07" WithProduct_CategoryID:@"创意" WithProduct_Price:@"799" WithProduct_MarketPrice:@"785" WithProduct_Description:@"创意百变高手系列 拼插积木玩具"];
    
    [[DBProduct sharedInstance] selectAllMethod];
    
    
    [[DBProductDetail sharedInstance] LinkDatabaseAndAddToQueue];
    [[DBProductDetail sharedInstance] insetProduct_Id:@"101" WithBrand:@"LEGO/乐高" WithTitle:@"乐高机械组 42056 保时捷911GT3 RSLEGO Technic 积木玩具收藏" WithPrice:@"2899" WithMarketPrice:@"3499" WithPlace:@"中国" WithModel:@"42056" WithCategory:@"成人" WithAge:@"16岁+" WithTopImage1:@"p01" WithTopImage2:@"p01" WithTopImage3:@"p01" WithTopImage4:@"p01" WithDetailImage1:@"p01" WithDetailImage2:@"p01" WithDetailImage3:@"p01" WithDetailImage4:@"p01"];
    [[DBProductDetail sharedInstance] insetProduct_Id:@"102" WithBrand:@"LEGO/乐高" WithTitle:@"乐高星球大战系列75192 豪华千年隼 LEGO积木玩具" WithPrice:@"988" WithMarketPrice:@"1088" WithPlace:@"中国台湾" WithModel:@"75192" WithCategory:@"儿童" WithAge:@"10岁-14岁" WithTopImage1:@"p02" WithTopImage2:@"p02" WithTopImage3:@"p02" WithTopImage4:@"p02" WithDetailImage1:@"p02" WithDetailImage2:@"p02" WithDetailImage3:@"p02" WithDetailImage4:@"p02"];
    [[DBProductDetail sharedInstance] insetProduct_Id:@"103" WithBrand:@"LEGO/乐高" WithTitle:@"乐高机械组 42068 机场救援车 LEGO Technical 积木玩具收藏" WithPrice:@"679" WithMarketPrice:@"759" WithPlace:@"越南" WithModel:@"42068" WithCategory:@"儿童" WithAge:@"10岁-14岁" WithTopImage1:@"p03" WithTopImage2:@"p03" WithTopImage3:@"p03" WithTopImage4:@"p03" WithDetailImage1:@"p03" WithDetailImage2:@"p03" WithDetailImage3:@"p03" WithDetailImage4:@"p03"];
    [[DBProductDetail sharedInstance] insetProduct_Id:@"104" WithBrand:@"LEGO/乐高" WithTitle:@"乐高机械组 420878 马克卡车MSCK An Them LEGO TECHNIC" WithPrice:@"1499" WithMarketPrice:@"1699" WithPlace:@"中国大陆" WithModel:@"42078" WithCategory:@"学龄前儿童" WithAge:@"0岁-4岁" WithTopImage1:@"p04" WithTopImage2:@"p04" WithTopImage3:@"p04" WithTopImage4:@"p04" WithDetailImage1:@"p04" WithDetailImage2:@"p04" WithDetailImage3:@"p04" WithDetailImage4:@"p04"];
    [[DBProductDetail sharedInstance] insetProduct_Id:@"105" WithBrand:@"LEGO/乐高" WithTitle:@"LEGO乐高 60138 城市警察总局 拼接积木 益智玩具" WithPrice:@"899" WithMarketPrice:@"1099" WithPlace:@"美国" WithModel:@"60138" WithCategory:@"城市" WithAge:@"5岁-12岁" WithTopImage1:@"p05" WithTopImage2:@"p05" WithTopImage3:@"p05" WithTopImage4:@"p05" WithDetailImage1:@"p05" WithDetailImage2:@"p05" WithDetailImage3:@"p05" WithDetailImage4:@"p05"];
    [[DBProductDetail sharedInstance] insetProduct_Id:@"106" WithBrand:@"LEGO/乐高" WithTitle:@"LEGO乐高 建筑系列21033 芝加哥 成人粉丝拼装建筑积木玩具收藏" WithPrice:@"389" WithMarketPrice:@"599" WithPlace:@"英国" WithModel:@"21033" WithCategory:@"建筑" WithAge:@"12岁-14岁" WithTopImage1:@"p06" WithTopImage2:@"p06" WithTopImage3:@"p06" WithTopImage4:@"p06" WithDetailImage1:@"p06" WithDetailImage2:@"p06" WithDetailImage3:@"p06" WithDetailImage4:@"p06"];
    [[DBProductDetail sharedInstance] insetProduct_Id:@"107" WithBrand:@"LEGO/乐高" WithTitle:@"LEGO乐高 创意百变高手系列 10259 冬季村庄车站 拼插积木玩具" WithPrice:@"785" WithMarketPrice:@"799" WithPlace:@"日本" WithModel:@"10259" WithCategory:@"创意" WithAge:@"12岁-14岁" WithTopImage1:@"p07" WithTopImage2:@"p07" WithTopImage3:@"p07" WithTopImage4:@"p07" WithDetailImage1:@"p07" WithDetailImage2:@"p07" WithDetailImage3:@"p07" WithDetailImage4:@"p07"];
    [[DBProductDetail sharedInstance] selectAllMethod];
    
    [[DBUser sharedInstance] LinkDatabaseAndAddToQueue];
    [[DBUser sharedInstance] insetUser_Name:@"13811112222" WithUser_PassWord:@"123456" WithUser_Type:@"管理员"];
    [[DBUser sharedInstance] insetUser_Name:@"13822223333" WithUser_PassWord:@"123456" WithUser_Type:@"普通用户"];
}



@end
